import {
  componentType as _componentType,
  componentList as _componentList,
} from "./data.js";

let componentType = _componentType;
let componentList = _componentList;
// 组件分类
const getComponentType = function() {
  return componentType;
};

// 组件列表
const getComponentList = function() {
  return componentList;
};

// 新增组件
const addComponent = function(data) {
  componentList.push(data);
};

// 编辑组件
const editComponent = function(id, detail) {
  const target = componentList.find((c) => c.id === id);
  target.name = detail.name;
  target.type = detail.type;
  target.shareCluster = detail.shareCluster;
  return componentList;
};

// 删除组件
const deleteComponent = function(id) {
  componentList = componentList.filter((c) => c.id !== id);
};

//按类型搜索组件
const searchComponent = function(type) {
  if (type === "ALL") {
    return componentList;
  }
  let newComponentList = componentList.filter(
    (c) => c.typeObject.value === type
  );
  return newComponentList;
};

//按名称搜索组件
const searchComponentByName = function(name) {
  console.log("searchComponentByName", name);
  // let newComponentList = componentList.filter((c) => c.typeObject.name.split(name).length > 1);
  let newComponentList = componentList.filter((c) => c.name.split(name).length > 1);
  return newComponentList;
};

// getComponentType: 组件管理 => 组件类型
// getComponentType: 组件管理 => 组件列表

export {
  getComponentList,
  deleteComponent,
  getComponentType,
  addComponent,
  editComponent,
  searchComponent,
  searchComponentByName,
};
